package com.myself.ssoserver.service

import com.myself.common.service.BaseService
import com.myself.ssoserver.entity.LoginHistories
import com.myself.ssoserver.entity.LoginHistory
import com.myself.ssoserver.repository.LoginHistoryRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
open class LoginHistoryService(
    private val repository: LoginHistoryRepository
) : BaseService<LoginHistory, LoginHistories>(repository) {

    @Transactional
    open fun recordLogin(userId: Int, ip: String, userAgent: String?) {
        addOne(createHistory(userId, ip, userAgent))
    }

    private fun createHistory(userId: Int, ip: String, userAgent: String?): LoginHistory {
        val loginHistory=  LoginHistory {
            this.userId = userId
            this.loginIp = ip
            this.userAgent = userAgent
        }
        return loginHistory
    }
}
